package st.business.tepth.business.visitor.visitorwork;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.adapter.TimeLineAdapter;
import st.business.tepth.business.bean.BusRepairBean;
import st.business.tepth.business.event.StartBrotherEvent;
import st.business.tepth.business.user.busnumber.workholder.dialog.DialogAllTrouble;
import st.business.tepth.business.user.busnumber.workholder.dialog.DialogContactWorker;
import st.business.tepth.business.visitor.visitorwork.work.WorkDelegate;
import st.shadow.library.core.base.mvp.MvpFragment;

/**
 * 游客的作业界面
 *
 * @author zhangyu
 * @date 2019/1/17
 */
public class VisitorWorkDelegate extends MvpFragment<VisitorWorkPresenter> implements VisitorWorkContract.View {
    public static VisitorWorkDelegate newInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        VisitorWorkDelegate delegate = new VisitorWorkDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_bus_licence)
    TextView mTvLicence;
    @BindView(R2.id.tv_bus_company)
    TextView mTvCompany;
    @BindView(R2.id.tv_register_time)
    TextView mTvRegisterTime;
    @BindView(R2.id.tv_bus_type)
    TextView mTvBusType;
    @BindView(R2.id.tv_bus_number)
    TextView mTvBusNumber;
    @BindView(R2.id.tv_driver_name)
    TextView mTvDriverName;
    @BindView(R2.id.tv_driver_code)
    TextView mTvDriverCode;
    @BindView(R2.id.tv_driver_phone)
    TextView mDriverPhone;
    @BindView(R2.id.tv_gas_use)
    TextView mTvGasUse;
    @BindView(R2.id.tv_oil_use)
    TextView mTvOilUse;
    @BindView(R2.id.tv_last_mileage)
    TextView mTvLastMileage;
    @BindView(R2.id.tv_total_mileage)
    TextView mTvTotalMileage;
    @BindView(R2.id.tv_work_group)
    TextView mTvWorkGroup;
    @BindView(R2.id.tv_work_type)
    TextView mTvWorkType;
    @BindView(R2.id.tv_line_name)
    TextView mTvLineName;
    @BindView(R2.id.layout_container)
    ConstraintLayout mLayoutContainer;
    private int mRepairId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("id");
        }
    }


    @Override
    public Object setLayout() {
        return R.layout.delegate_visitor_work;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        // LinearSnapHelper snapHelper = new LinearSnapHelper();
        // snapHelper.attachToRecyclerView(mRecyclerView);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        addTopView(R.layout.view_loading, mLayoutContainer);
        mPresenter.getBusInfo(mRepairId);
    }

    @Override
    public VisitorWorkPresenter getPresenter() {
        return new VisitorWorkPresenter(this);
    }

    @Override
    public void showBusInfo(BusRepairBean visitorRepairBean) {
        if (visitorRepairBean != null) {
            mTvLicence.setText(visitorRepairBean.getLicense_no());
            mTvCompany.setText(String.format("所属公司：%s",
                    visitorRepairBean.getOrganization_name()));
            mTvRegisterTime.setText(String.format("登记日期：%s",
                    visitorRepairBean.getRecord_date()));
            mTvBusType.setText(String.format("车辆型号：%s",
                    visitorRepairBean.getVehicle_model_no()));
            mTvBusNumber.setText(String.format("车辆编号：%s",
                    visitorRepairBean.getVehicle_no()));
            mTvDriverName.setText(String.format("司机：%s",
                    visitorRepairBean.getUser_name()));
            mDriverPhone.setText(String.format("电话：%s",
                    visitorRepairBean.getMobile_no()));
            mTvDriverCode.setText(String.format("工号：%s",
                    visitorRepairBean.getJob_no()));
            mTvWorkGroup.setText(String.format("作业班组：%s",
                    visitorRepairBean.getGroupName()));
            mTvWorkType.setText(String.format("维修类型：%s",
                    visitorRepairBean.getTypeName()));
            mTvLineName.setText(String.format("车辆线路：%s",
                    visitorRepairBean.getLine_no()));
            String gasType = visitorRepairBean.getFuelType();
            String fuelPerKm = visitorRepairBean.getFuelPerKm();
            String engineOil = visitorRepairBean.getEngineOil();
            String kmAfterLastVm = visitorRepairBean.getKmAfterLastVm();
            String kmTotal = visitorRepairBean.getKmTotal();
            mTvGasUse.setText(String.format("上月%s耗量：%s", gasType, fuelPerKm));
            mTvOilUse.setText(String.format("上月机油耗量：%s", engineOil));
            mTvLastMileage.setText(String.format("距上次保养里程：%s", kmAfterLastVm));
            mTvTotalMileage.setText(String.format("总里程：%s", kmTotal));
            List<BusRepairBean.RepairAuditListBean> repairAuditListBeans =
                    visitorRepairBean.getRepairAuditList();
            if (repairAuditListBeans != null && !repairAuditListBeans.isEmpty()) {
                mRecyclerView.setLayoutManager(new LinearLayoutManager
                        (_mActivity, RecyclerView.HORIZONTAL, false));
                mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
                    @Override
                    public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int i) {
                        BusRepairBean.RepairAuditListBean repairAuditListBean =
                                (BusRepairBean.RepairAuditListBean) adapter.getItem(i);
                        if (i == 0) {
                            new DialogAllTrouble(_mActivity, mRepairId).show();
                        } else {
                            EventBus.getDefault().post(new StartBrotherEvent(
                                    WorkDelegate.newInstance(mRepairId, "作业中")
                            ));
                        }
                    }
                });
                mRecyclerView.setAdapter(new TimeLineAdapter
                        (_mActivity, R.layout.item_work_time_line, repairAuditListBeans));
            }
            removeTopView(mLayoutContainer);
        } else {
            removeTopView(mLayoutContainer);
            addTopView(R.layout.view_bus_empty, mLayoutContainer);
        }
    }

    @OnClick(R2.id.tv_contact_worker)
    public void onContactWorker() {
        new DialogContactWorker(_mActivity, mRepairId).show();
    }
}
